<?php
/**
 * Created by PhpStorm.
 * User: macp
 * Date: 2017/1/10
 * Time: 14:25
 */

namespace App\Repositories;


class CommonRepository
{
	protected $model;

	public function __construct($model)
	{
		$this->model = $model;
	}

	/**
	 * 查询分页列表
	 * @author macp
	 * @param array $condition 条件筛选
	 * @param array $column 查询字段
	 * @param int $page 查询页数
	 * @param string $sortBy
	 * @param string $sort
	 * @return array
	 */
	public function pageList($condition = [], $column = ['*'], $pageNum = 12, $sortBy = 'id', $sort = 'DESC')
	{
		return $this->model->where($condition)->orderBy($sortBy, $sort)->paginate($pageNum, $column);
	}

	/**
	 * 增加数据
	 * @author macp
	 * @param array $data 数据
	 * @return boolean
	 */
	public function add($data = [])
	{
		return $this->model->create($data);
	}

	/**
	 * 根据条件删除数据
	 * @author macp
	 * @param array $condition 条件
	 * @return boolean
	 */
	public function destory($condition = [])
	{
		return $this->model->where($condition)->delete();
	}

	/**
	 * 根据条件获取单条数据
	 * @author macp
	 * @param array $condition 条件
	 * @param array $column 查询字段
	 * @return object
	 */
	public function getOne($condition = [], $column = ['*'])
	{
		return $this->model->where($condition)->first($column);
	}

	/**
	 * 根据条件更新数据
	 * @author macp
	 * @param array $condtion 条件
	 * @param array $data 数据
	 * @return boolean
	 */
	public function edit($condtion = [], $data = [])
	{
		return $this->model->where($condtion)->update($data);
	}

	/**
	 * 根据条件获取数据
	 * @author macp
	 * @param array $condition 条件
	 * @param array $column 查询字段
	 * @param string $orderBy 排序字段
	 * @param string $order 排序规则 DESC,ASC
	 * @return array
	 */
	public function getList($condition = [], $column = ['*'], $orderBy = 'id', $order = 'DESC')
	{
		return $this->model->where($condition)->orderBy($orderBy, $order)->get($column);
	}

	/**
	 * 获取特定的key value
	 * @auhtor macp
	 * @param array $condtion 条件
	 * @param $column 字段
	 * @param null $key key健
	 * @return array
	 */
	public function pluck($condtion = [], $column, $key = null)
	{
		return $this->model->where($condtion)->pluck($column, $key);
	}
}
